//
//  PieChart.m
//  edu
//
//  Created by baobaodp on 13-11-15.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "PieChart.h"
#import "Record.h"
#import "SeriesItem.h"
#import "RecordField.h"

@implementation PieChart


-(NSString *) getDataSource
{
    SeriesItem *serieInfo = [self.serieses objectAtIndex:0];
    
    NSMutableString *tmp = [NSMutableString stringWithCapacity:8];
    
    [tmp appendString:@"var pieData = [ "];
    
    for (Record * data in self.dataSource) {
        [tmp appendString:@"{"];
        
        [tmp appendFormat:@"'category': '%@',",[data getFieldValueByName:serieInfo.name]];
        [tmp appendFormat:@"'value': %@",[data getFieldValueByName:serieInfo.field]];
        
        [tmp appendString:@"},"];
        
    }
    [tmp appendString:@"];"];
    
    
    return tmp;
}

-(NSString *) createChartScript
{
    
    NSString * script = @"function createChart() {"
        "$('#chart').kendoChart({"
            "chartArea: {"
                "width: 440"
            "},"
            "theme:'silver',"
            "title: {"
                "text: '%@'"
            "},"
            "legend: {"
                "position: 'top'"
            "},"
            "seriesDefaults: {"
                "labels: {"
                    "template: \"#= category # - #= kendo.format('{0:P}', percentage)#\","
                    "position: 'outsideEnd',"
                    "visible: true,"
                    "background: 'transparent'"
                "}"
            "},"
            "series: [{"
                "type: 'pie',"
                "data: pieData"
            "}],"
            "tooltip: {"
                "visible: true,"
                "template: \"#= category # - #= kendo.format('{0:P}', percentage) #\" "
            "}"
        "});"
      "$(document).scrollLeft(70);"
    "}";
    return [NSString stringWithFormat:script,self.title] ;
}


@end
